﻿
using System.Collections.ObjectModel;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using Telerik.Windows.Controls;
using System.ComponentModel;
using System;
using System.Linq;
using System.Windows.Input;

namespace Rio.Framework.Pages.ChildWindows
{
    public class GroupWindowBase : RadWindow, INotifyPropertyChanged
    {
        #region Attributs

        private ObservableCollection<Group> _availableGroups = new ObservableCollection<Group>();
        private ObservableCollection<Group> _addedGroups = new ObservableCollection<Group>();

        public List<Group> m_szSelectedItems { get; set; }
        protected List<Group> m_oGroupList { get; set; }
        protected string m_sGroupListToString = string.Empty;
        protected bool _groupRestrict = false;
        protected List<Group> _displayedGroupList;
        protected List<long> _groupListId { get; set; }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the available groups.
        /// </summary>
        /// <value>The available groups.</value>
        public ObservableCollection<Group> AvailableGroups
        {
            get
            {
                return _availableGroups;
            }
            set
            {
                _availableGroups = value;
                OnPropertyChanged("AvailableGroups");
            }
        }

        /// <summary>
        /// Gets or sets the selected groups.
        /// </summary>
        /// <value>The selected groups.</value>
        public ObservableCollection<Group> AddedGroups
        {
            get
            {
                return _addedGroups;
            }
            set
            {
                _addedGroups = value;
                OnPropertyChanged("AddedGroups");
            }
        }

        /// <summary>
        /// Gets or sets the input value.
        /// </summary>
        /// <value>The input value.</value>
        public virtual string InputValue { get; set; }

        #endregion

        #region Events


        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Constructors 

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupWindowBase" /> class.
        /// </summary>
        public GroupWindowBase()
        {
            this.CanMove = false;
            this.ResizeMode = Telerik.Windows.Controls.ResizeMode.NoResize;
            this.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;

            this.KeyUp += MessageBoxChildWindow_KeyUp;
        }

                /// <summary>
        /// Initializes a new instance of the <see cref="GroupFilterWindow" /> class.
        /// </summary>
        /// <param name="p_sGroupList">The P_S group list.</param>
        /// <param name="p_bGroupRestrict">The P_B group restrict.</param>
        public GroupWindowBase(string groupList, bool groupRestrict = false)
            : this()
        {
            this._groupRestrict = groupRestrict;
            this.m_sGroupListToString = groupList;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupFilterWindow" /> class.
        /// </summary>
        /// <param name="p_oGroupList">The p_o group list.</param>
        /// <param name="p_bGroupRestrict">The P_B group restrict.</param>
        public GroupWindowBase(List<Group> groupList, bool groupRestrict = false)
            : this()
        {
            this._groupRestrict = groupRestrict;
            this.m_sGroupListToString = String.Join(";", groupList.Select(item => item.Name));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupFilterWindow" /> class.
        /// </summary>
        /// <param name="p_oGroupIdList">The p_o group id list.</param>
        /// <param name="p_bGroupRestrict">The P_B group restrict.</param>
        public GroupWindowBase(List<long> groupIdList, bool groupRestrict = false)
            : this()
        {
            this._groupListId = groupIdList;
            this._groupRestrict = groupRestrict;
        }

        #endregion

        /// <summary>
        /// Handles the KeyUp event of the MessageBoxChildWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs" /> instance containing the event data.</param>
        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        /// <summary>
        /// This method  is called to filter listbox values with a "starting with" constraint
        /// </summary>
        /// <param name="FilterTerm"></param>
        /// <returns></returns>
        protected IEnumerable<Group> FilterItems(string FilterTerm)
        {
            IEnumerable<Group> result = null;
            result = _displayedGroupList;

            if (!string.IsNullOrEmpty(FilterTerm))
            {
                result = result.Where(f => f.Name.ToLower().Contains(FilterTerm.ToLower()));
            }

            return result;
        }
    }
}
